﻿using KYJBLL;
using KYJModels;
using KYJScadaProModbusRtu;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace KYJScadaProModbusRTU
{
    public partial class FrmAlarmLog : Form
    {
        public FrmAlarmLog()
        {
            InitializeComponent();
        }

        private SysLogManage sysLogManage = new SysLogManage();

        //当前时间
        private string CurrentTime
        {
            get { return DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); }
        }

        #region 通用的添加系统日志
        public void AddLog(bool isError, string info)
        {
            if (dgv_Log.InvokeRequired)   //如果存在跨线程访问
            {
                this.dgv_Log.Invoke(new Action<bool, string>(AddLog), isError, info);
            }
            else
            {
                int rowCount = this.dgv_Log.Rows.Add();
                this.dgv_Log.Rows[rowCount].Cells[0].Value = CurrentTime;
                this.dgv_Log.Rows[rowCount].Cells[1].Value = isError ? "错误" : "信息";
                this.dgv_Log.Rows[rowCount].Cells[2].Value = info;
            }
            //存储到数据库
            sysLogManage.AddSysLog(new SysLog()
            {
                InsertTime = CurrentTime,
                LogType = "系统日志",
                Note = info,
                AlarmType = isError ? "错误" : "信息",
                Operater = CommomMethods.currentAdmin.LoginName
            });
        }
        #endregion

        #region 通用添加操作日志
        public void AddOPLog(bool isError, string info)
        {
            if (this.dgv_OPLog.InvokeRequired)
            {
                this.dgv_OPLog.Invoke(new Action<bool, string>(AddLog), isError, info);
            }
            else
            {
                int rowCount = this.dgv_OPLog.Rows.Add();
                this.dgv_OPLog.Rows[rowCount].Cells[0].Value = CurrentTime;
                this.dgv_OPLog.Rows[rowCount].Cells[1].Value = isError ? "错误" : "信息";
                this.dgv_OPLog.Rows[rowCount].Cells[2].Value = info;
            }
            //存储到数据库
            sysLogManage.AddSysLog(new SysLog()
            {
                InsertTime = CurrentTime,
                LogType = "操作日志",
                Note = info,
                AlarmType = isError ? "错误" : "信息",
                Operater = CommomMethods.currentAdmin.LoginName
            });
        }
        #endregion

        #region 通用的添加报警信息
        public void AddAlarm(SysLog sysLog)
        {
            if (this.dgv_Alarm.InvokeRequired)
            {
                this.dgv_Alarm.Invoke(new Action<SysLog>(AddAlarm), sysLog);
            }
            else
            {
                int rowCount = this.dgv_Alarm.Rows.Add();
                this.dgv_Alarm.Rows[rowCount].Cells[0].Value = sysLog.InsertTime;
                this.dgv_Alarm.Rows[rowCount].Cells[1].Value = sysLog.AlarmType;
                this.dgv_Alarm.Rows[rowCount].Cells[2].Value = sysLog.Note;
            }
            //存储到数据库
            sysLogManage.AddSysLog(sysLog);
        }
        #endregion

        private void dgv_Log_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            DataGridViewHelper.DgvRowPostPaint((DataGridView)sender, e);
        }

        #region 减少闪烁
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams parss = base.CreateParams;
                parss.ExStyle |= 0x02000000;
                return parss;
            }
        }
        #endregion
    }
}
